<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" :labelWidth="100" :collapse="false" @onCollapse="collapseHandle" @filterChange="handleFilterChange" />
    <FilterTable
      ref="mainTable"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :columns="tableColumns"
      :isSelectColumn=false
      columnsRef="aduitOrder"
      :datakey="'records'"
      :onRowSelectChange="handleRowSelectChange"
      :onColumnsChange="columns => (this.tableColumns = columns)"
      :isExportExcel2="true"
      :exportFileName="'采购单清单.xlsx'"
    >
      <!-- <template slot="controls">
        <AjaxButton size="small" type="primary" :click="check.bind(this, 2)">审核通过</AjaxButton>
        <AjaxButton size="small" type="primary" :click="check.bind(this, 3)">审核驳回</AjaxButton>

      </template> -->
    </FilterTable>
    <div>
      <BaseDialog width="80%" title="采购单明细" :visible.sync="isShowpartBuyItems" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <partBuyItems :top="top" @cancel="isShowpartBuyItems = false" />
      </BaseDialog>
      <BaseDialog title="经销商" :visible.sync="isShowSearchCustomer" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <SearchCustomer :customerNo="kehuxuanze" @close="isShowSearchCustomer = false" @addCustomerCode="confirmSearchCustomer" />
      </BaseDialog>
      <!-- <BasePrint ref="Salesprint" :data="SalesprintList" template="templatepartInventory" /> -->
    </div>
  </div>
</template>

<script>
import { queryPartOnlySelect,queryAduit} from '@/api/partsmanage/purchaseOrderquery/query';
import { printData, getDealerUser, exportData, partbuyCheck } from '@/api/part/partInventory/partInventory/index.js';
import { dictionary } from '@/utils/dictMixin';
import SearchCustomer from './SearchCustomer';
import { byAllTranslate } from '@/api/repair/batchEventManagement/activityDistribution';
import { exportExcelFile } from '@/utils/index';
import { Checkbox } from 'element-ui';
import partBuyItems from './partBuyItems';
import {ddlxArr, ddztArr, ddtblxArr} from './constant';

export default {
  name: 'aduitOrder',
  mixins: [dictionary],
  components: {
    partBuyItems,
    SearchCustomer
  },
  data() {
    return {
      top: {},
      SalesprintList: {},
      cunUserList: [],
      rowData: {},
      cunPrintid: '',
      kehuxuanze: '',
      selectList: [],
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      isShowpartBuyItems: false,
      isShowSearchCustomer: false,
      prindfBtnDisabled: true,
      fetchapi: queryPartOnlySelect,
      // fetchapi: queryAduit,
      fetchParams: { xhrAbort: true }
    };
  },
  mounted() {
    // this.getDealerUser();
    // this.translateDealer();
  },
  methods: {
    createTopFilterList() {
      return [
        // {
        //   type: 'INPUT',
        //   label: '经销商代码',
        //   fieldName: 'customerCode',
        //   readonly: true,
        //   unitRender: () => {
        //     return (
        //       <el-button
        //         icon="el-icon-search"
        //         onClick={() => {
        //           this.isShowSearchCustomer = true;
        //         }}
        //       ></el-button>
        //     );
        //   },
        //   onEnter: val => {
        //     console.log('对方经销商 onEnter');
        //     this.isShowSearchCustomer = true;
        //   }
        // },
        {
          type: 'INPUT',
          label: '服务商代码',
          fieldName: 'ownerCode',
          // placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '服务商名称',
          fieldName: 'customerName',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '备件订单号',
          fieldName: 'purchaseCode',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          // fieldName: 'purchaseType',
          fieldName: 'ddlxArr',
          itemList: ddlxArr,
          placeholder: '请选择...'
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'submStartDate|submEndDate',
          label: '提报日期',
          initialValue: [],
          dateType: 'daterange'
        },

        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'purchaseStatus',
          itemList: ddztArr,
          placeholder: '请选择...'
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'auditStartDate|auditEndDate',
          label: '审核日期',
          initialValue: [],
          dateType: 'daterange'
        },

      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          // sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          render: params => {

            return (
              <div>
                <el-button  type="text" size="mini" onClick={() => this.showItems(params.row)}>
                  查看明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '服务商代码',
          dataIndex: 'DEALER_CODE',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '服务商名称',
          dataIndex: 'DEALER_NAME',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '备件订单号',
          dataIndex: 'XSDDHM',
          filter: true,
          sorter: true,
          filterType: 'input'
        },

        {
          title: '订单类型',
          dataIndex: 'XSDDLX',
          filter: true,
          sorter: true,
          filterItems: ddlxArr,
          dictItems: ddlxArr,
          filterType: 'checkbox'
        },
        {
          title: '订购数量',
          dataIndex: 'BSZSLS',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '订单金额',
          dataIndex: 'XSYSJE',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '订单状态',
          dataIndex: 'XSDDZT',
          filter: true,
          sorter: true,
          filterItems: ddztArr,
          dictItems: ddztArr,
          filterType: 'checkbox'
        },
        {
          title: '是否免运费',
          dataIndex: 'DDMYF',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.DDMYF, '1004')}</span>;
          }
        },
        {
          title: '提报人',
          dataIndex: 'TB_MAN',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '订单提报日期',
          dataIndex: 'DDTBSJ',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '创建日期',
          dataIndex: 'CREATED_AT',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '审核日期',
          dataIndex: 'DDSPSJ',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },



      ];
    },
    collapseHandle() {},
    handleFilterChange(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, {
        ...e,
        xhrAbort: false,
        submStartDate: e.submStartDate ? e.submStartDate.split(' ')[0] : '',
        submEndDate: e.submEndDate ? e.submEndDate.split(' ')[0] : '',
        auditStartDate: e.auditStartDate ? e.auditStartDate.split(' ')[0] : '',
        auditEndDate: e.auditEndDate ? e.auditEndDate.split(' ')[0] : '',
        dealerCode: e.customerCode ? this.rowData.dealerCode : ''
      });
    },

    async confirmSearchCustomer(row) {
      console.log(row, 'rowCustomer');
      this.rowData = row[0];
      console.log('row[0].dealerCode', row[0].dealerCode);
      this.topFilterList.find(x => x.fieldName == 'customerCode').initialValue = row[0].dealerCode;
      this.topFilterList.find(x => x.fieldName == 'customerName').initialValue = row[0].dealerName;
    },
    // async translateDealer() {
    //   let res = await byAllTranslate();
    //   if (res.resultCode == 200) {
    //     // console.log(res.data, 'res.data::');
    //     let dataList = res.data ? res.data : [];
    //     // console.log(dataList, 'dataList::');
    //     let tempList = dataList.map(x => ({
    //       value: x.dealerCode,
    //       text: x.dealerName
    //     }));
    //     // console.log(tempList, 'tempList::');
    //     this.tableColumns.find(x => x.dataIndex == 'ownerCode').dictItems = tempList;
    //   }
    // },
    handleRowSelectChange(rows) {
      this.selectList = rows;
    },
    showItems(e) {
      this.isShowpartBuyItems = true;
      this.top = e;
    },

  }
};
</script>
